package com.handu.luna.mall.controller;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.handu.luna.core.domain.promotion.Topic;

/**
 * -------------------------------------- TopicController
 * 
 * @Function "专题页"的controller
 * @author 邹永禹 QQ:3043441459
 * @date 2014年9月1日 下午2:40:11 --------------------------------------
 */
@Controller
public class TopicController extends BaseController {

	@RequestMapping("/topic/{topicname}.html")
	public MallModelView index(@PathVariable("topicname") String fileName) {
		
		Topic topic = context.getTopicRepository().findByFileNameForPortal(fileName);
		
		// 专题查询不到 重定向到404页面
		if (topic == null) {
			throw new PageNotFoundException("专题页不存在");
		}
		
		//设置了url的专题 直接重定向
		if(StringUtils.isNotBlank(topic.getUrl())){
			return this.buildModelView("redirect:"+topic.getUrl());
		}
		
		topic.setTopicModuleList(topic.handleTopicModule(topic.getTopicModuleList()));
		
	    // 根据每个专题创建时 选择的模版 获取模版名称 return buildModelView("/topic/" +
 	    // topic.getTemplateFile()).add("topic", topic);
		//return buildModelView("/active/topic_for_html").add("topic", topic);
		return buildModelView("/active/topic_auto").add("topic", topic);
		
	}
}
